<?php
 
	/*
	 * Created by Nicky
	 *
	*/
	 /* permission name 
	 karPenilaian_Student
	 karPenilaian_Student_add
	 karPenilaian_Student_edit
	 */
	class Report_presensiController extends CController{
		private $result = array("actionStatus"=>0,"actionMessage"=>"","model"=>array(),"actionError"=>array());	
		public $mainMenu = "Presensi";
		public $subMenu = "";
		public $title = "";		
		public $loginUser;		
		public function init() {
			parent::init();
			if (!LoginUser::isLogin("orangtua")){
				LoginUser::redirectToLoginPage("orangtua");
			}else{
				$this->loginUser = LoginUser::getUser();
			}
						
		} 
		
		public function actionByStudent(){
			LoginUser::getPermission("akademik_presensi_student");
			 
			$this->layout ="//layouts/ly_blank";
				$this->layout ="//layouts/ly_blank";
			$student = $this->loginUser ->student;
	 		$kelas_tahun_ajaran_id = Yii::app()->request->getParam("kelas_tahun_ajaran_id","");	
			$student_id=$student->id;	
			$department_id=$student->department_id;				
						
 			$module= $this->loginUser ->module;
			$absent_date = Yii::app()->request->getParam("absent_date","");				
			$absent_date_end = Yii::app()->request->getParam("absent_date_end","");				
			$name_tahunajaran = Yii::app()->request->getParam("name_tahunajaran","");							
			
			$datenow=date("Y-m-d");
			$tahunajarans = MTahunAjaran::model()->findAll("is_active = TRUE   and department_id=:departmentId and (:datenow between start_date and end_date)  order by id",array(":departmentId"=>$department_id,":datenow"=>$datenow));

			if ($tahunajarans == null){
				$tahun_ajaran_id == "";
			}else{	
				$tahun_ajaran_id = Yii::app()->request->getParam("tahun_ajaran_id", $tahunajarans[0]->id);
				$absent_date = $absent_date == "" ? date('d-M-Y',strtotime($tahunajarans[0]["start_date"])) : $absent_date;				
				$absent_date_end = $absent_date_end == "" ? date('d-M-Y',strtotime($tahunajarans[0]["end_date"])) : $absent_date_end;				
				$name_tahunajaran = $name_tahunajaran == "" ? $tahunajarans[0]["name"] : $name_tahunajaran;								
			}
			$this->subMenu = $name_tahunajaran;
 		 
			$kelass = VStudentKelasTahunAjaran::model()->findAll("student_id=:student_id and tahun_ajaran_id=:tahun_ajaran_id  ",array(":student_id"=>$student_id,":tahun_ajaran_id"=>$tahun_ajaran_id));
 
			if ($kelass == null){
				$kelas_tahun_ajaran_id == "";
			}else{
				$kelas_tahun_ajaran_id = $kelas_tahun_ajaran_id == "" ? $kelass[0]["kelas_tahun_ajaran_id"] : $kelas_tahun_ajaran_id;
			}
		 
 
			$presensi = MPresensi::model()->findAll("kelas_tahun_ajaran_id=:kelas_tahun_ajaran_id and is_active = TRUE  order by id",array(":kelas_tahun_ajaran_id"=>$kelas_tahun_ajaran_id)); 
			if ($presensi == null){
				$presensi_id=0;
			}else{
				$presensi_id = $presensi[0]["id"];
			}
		 
			$data=array(
 					"student_id"=>$student_id,
					"absent_date"=>$absent_date,					
					"absent_date_end"=>$absent_date_end,										
					"kelas_tahun_ajaran_id"=>$kelas_tahun_ajaran_id
 				);				
			$this->render('rptByStudent',$data);	
 		}
		
		public function actionByStudentList(){		
			$kelas_tahun_ajaran_id = Yii::app()->request->getParam("kelas_tahun_ajaran_id","");
			$student_id = Yii::app()->request->getParam("student_id","");			
			$absent_date = Yii::app()->request->getParam("absent_date");
			$absent_date_end = Yii::app()->request->getParam("absent_date_end");			
			$absent_date = date_format(date_create_from_format( "d-M-Y", $absent_date),"Y-m-d");	
			$absent_date_end = date_format(date_create_from_format( "d-M-Y", $absent_date_end),"Y-m-d");		
 		

			$mcon= Yii::app()->db;
			$sqlall='SELECT B.id as student_id, B.sid, P.id as presensi_id,  PS.id as id,P.karyawan_id,P.kelas_tahun_ajaran_id,PS.presensi_status,PS.notes,PS.student_id,PS.absent_date,MONTHNAME((PS.absent_date)) as months,B.name FROM  `m_presensi` P left join m_presensi_student PS on P.id=PS.presensi_id left join v_student_kelas_tahun_ajaran  VSKTA on VSKTA .student_id = PS.student_id and P.kelas_tahun_ajaran_id=VSKTA.kelas_tahun_ajaran_id 
left join m_student B on PS.student_id=B.id where  PS.id is not null  ';
			$sqltask_count = $sqlall . " and P.kelas_tahun_ajaran_id=:kelas_tahun_ajaran_id and B.id =:student_id and  ( PS.absent_date between :absent_date and :absent_date_end) order by PS.absent_date";

			$mReportMonthCommands=$mcon->createCommand($sqltask_count);		
			$mReportMonthCommands->bindValue('absent_date',$absent_date);		
			$mReportMonthCommands->bindValue('absent_date_end',$absent_date_end);
			$mReportMonthCommands->bindValue('kelas_tahun_ajaran_id',$kelas_tahun_ajaran_id); 		
			$mReportMonthCommands->bindValue('student_id',$student_id); 										
			$mReportMonthCommands=$mReportMonthCommands->queryAll();		
			$this->result["actionStatus"] =0;
			$this->result["actionMessage"] ="";
			$this->result["actionError"] =array();
			$this->result["model"] = $mReportMonthCommands;			
			header('Content-type:application/json');
			echo CJSON::encode($this->result);
		}		 
				
	protected function beforeRender($view){
		if ($this->subMenu != ""){
			$this->title = $this->mainMenu . " - " .$this->subMenu;
		}
		return true;
	}
		
	public function actionPopupWindow()
	{
		$this->layout = "//layouts/ly_blank";
		$this->render('popupWindow');
	}		
		
	}
?>